package helper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import activities.StatisticsActivity.ViewState;
import android.util.Log;

public class TimeSpan {

	public final static String DATE_FORMAT = "yyyy-MM-dd";

	public Date startDate;
	public Date endDate;
	public Date today;

	public TimeSpan(ViewState currentViewState) {

		Calendar c = Calendar.getInstance();
		today = new Date();
		if (currentViewState == ViewState.Day) {

			c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
			
			startDate = c.getTime();
			c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			endDate = c.getTime();
			Log.i("DB-TEST", getDateAsString(startDate));
			Log.i("DB-TEST", getDateAsString(endDate));
		}
		
		if (currentViewState == ViewState.Month) {

			c.set(Calendar.MONTH, Calendar.JANUARY);			
			startDate = c.getTime();
			c.set(Calendar.MONTH, Calendar.DECEMBER);
			endDate = c.getTime();
			
			Log.i("DB-TEST", getDateAsString(startDate));
			Log.i("DB-TEST", getDateAsString(endDate));
		}
		
		

	}

	public TimeSpan(ViewState currentViewState, Date date) {
		today = new Date();
		if (currentViewState == ViewState.Day) {

			Calendar c = Calendar.getInstance();

			endDate = date;

			c.setTime(endDate);
			c.add(Calendar.DATE, -6);
			startDate = c.getTime();
			Log.i("DB-TEST", getDateAsString(startDate));
			Log.i("DB-TEST", getDateAsString(endDate));
		}
	}


	public int daysBetween(Date d1, Date d2) {
		return (int) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
	}

	/**
	 * Decreases the current timespan. It depends on the ViewState how much
	 * (=how many days) is decreased
	 * 
	 * @param currentViewState
	 */
	public void decreaseTimeSpan(ViewState currentViewState) {
		if (currentViewState == ViewState.Day) {

			Calendar c = Calendar.getInstance();
			c.setTime(startDate);
			c.add(Calendar.DATE, -7);

			startDate = c.getTime();
			c.setTime(endDate);
			c.add(Calendar.DATE, -7);
			endDate = c.getTime();
		}
		if(currentViewState == ViewState.Month){
			Calendar c = Calendar.getInstance();
			c.setTime(startDate);
			c.add(Calendar.YEAR, -1);
			startDate = c.getTime();
			
			 c = Calendar.getInstance();
			c.setTime(endDate);
			c.add(Calendar.YEAR, -1);
			endDate = c.getTime();

		}

	}

	/**
	 * Increases the current timespan. It depends on the ViewState how much
	 * (=how many days) is increased
	 * 
	 * @param currentViewState
	 */
	public void increaseTimeSpan(ViewState currentViewState) {
		if (currentViewState == ViewState.Day) {

			Calendar c = Calendar.getInstance();
			c.setTime(startDate);
			c.add(Calendar.DATE, 7);

			startDate = c.getTime();
			c.setTime(endDate);
			c.add(Calendar.DATE, 7);
			endDate = c.getTime();
		}
		if(currentViewState == ViewState.Month){
			Calendar c = Calendar.getInstance();
			c.setTime(startDate);
			c.add(Calendar.YEAR, 1);
			startDate = c.getTime();
			
			 c = Calendar.getInstance();
			c.setTime(endDate);
			c.add(Calendar.YEAR, 1);
			endDate = c.getTime();

		}

	}

	public String getDateAsString(Date d) {
		SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMAT);
		return sf.format(d);
	}


}
